package com.yzy.dao;

import com.yzy.entity.Admins;
import com.yzy.entity.Doctors;
import com.yzy.utils.Utils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminDao {
    private Utils util = new Utils();
    public  int add(Admins admin){
        String sql="insert into admin"+"values(?,?)";
        PreparedStatement ps =util.createStatement(sql);
        int result =0;
        try {
            ps.setString(1,admin.getAid());
            ps.setString(2,admin.getApass());


            result =ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close();
        }
        return result;
    }
    public List findAll(){
        String sql = "select * from admin";
        PreparedStatement ps = util.createStatement(sql);
        ResultSet rs = null;
        List adminList = new ArrayList();
        try {
            rs = ps.executeQuery();

            while (rs.next()){

                String aid=rs.getString("aid");
                String apass=rs.getString("apass");
                Admins admins =new Admins(aid,apass);
                adminList.add(admins);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(rs);
        }
        return adminList;
    }
    public List findAid(){
        String sql = "select aid from admin";
        PreparedStatement ps = util.createStatement(sql);
        ResultSet rs = null;
       List<String> aidlist=new ArrayList();
        try {
            rs = ps.executeQuery();

            while (rs.next()){

                String aid=rs.getString("aid");
                aidlist.add(aid);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(rs);
        }
        return aidlist;
    }
    public List findApass(){
        String sql = "select apass from admin";
        PreparedStatement ps = util.createStatement(sql);
        ResultSet rs = null;
        List<String> apasslist=new ArrayList();
        try {
            rs = ps.executeQuery();

            while (rs.next()){

                String apass=rs.getString("apass");
                apasslist.add(apass);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(rs);
        }
        return apasslist;
    }
    //登陆验证
    public  int longin(String adminid,String adminpass){
        String sql = "select count(*)from admin where aid=? and apass=?";
        PreparedStatement ps=util.createStatement(sql);
        ResultSet rs=null;
        int result = 0;
        try {
            ps.setString(1,adminid);
            ps.setString(2,adminpass);
            rs=ps.executeQuery();
            while (rs.next()){
                result=rs.getInt("count(*)");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(rs);
        }return result;
    }
}
